from django.db import models
import json

# database command 
# 1 manage.py validate
# 2 manage.py sqlall app_name
# 3 manage.py syncdb

# Create your models here.
class ImageItem(models.Model):
	name = models.CharField(max_length = 100)
	depthOffset = models.FloatField()
	depthResize = models.FloatField()
	focalLength = models.FloatField()
	sensorWidth = models.FloatField()
	
	def __str__(self):
		return self.name
	def Meta():
		ordering = ["name"]
	
class PixelPosition(models.Model):
	xPos = models.FloatField()
	yPos = models.FloatField()
	depth = models.FloatField()
	normalX = models.FloatField()    #normal in brg [0 255] format
	normalY = models.FloatField()
	normalZ = models.FloatField()
	imageItem = models.ForeignKey(ImageItem)
	

def updateDatabase(json_file):
	json_data = open(json_file, 'r')
	data = json.load(json_data)
	imageItem = ImageItem(name = data['name'], depthOffset = data['depthOffset'], depthResize = data['depthResize'], focalLength = data['focalLength'], sensorWidth = data['sensorWidth'])    
	imageItem.save()
	for pn in data['positionNormals']:
		pix = PixelPosition(xPos = pn[0], yPos = pn[1], depth = pn[2], normalX = pn[3], normalY = pn[4], normalZ = pn[5], imageItem = imageItem)
		pix.save()
    #json_data.close();
	
	

	
	
